package com.bci.pwtz.controller.app;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bci.pwtz.common.DeviceDictConstants;
import com.bci.pwtz.common.PwtzConstants;
import com.bci.pwtz.common.remote.ListResponse;
import com.bci.pwtz.common.remote.ObjResponse;
import com.bci.pwtz.common.remote.ReturnCode;
import com.bci.pwtz.common.remote.req.BaseIdReq;
import com.bci.pwtz.common.remote.req.BaseListReq;
import com.bci.pwtz.common.remote.req.ZydlzdObjReq;
import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.controller.AbstractController;
import com.bci.pwtz.exception.CanNotDeleteException;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.model.Zydlzd;
import com.bci.pwtz.service.DictionaryAuditService;
import com.bci.pwtz.service.ZydlzdService;
@Controller
@RequestMapping("/api/zydlzd")
public class ZydlzdAppController extends AbstractController
{
@Autowired
ZydlzdService service;
@Autowired
DictionaryAuditService daservice;
@RequestMapping(value = "/list", method = RequestMethod.POST)
@ResponseBody
public ListResponse<Zydlzd> list(HttpServletRequest request)
{
    BaseListReq req = null;
    ListResponse<Zydlzd> resp = new ListResponse<Zydlzd>();
    PaginationList<Zydlzd> list = new PaginationList<Zydlzd>();
    try
    {
        req = (BaseListReq) getJsonToObj(request, BaseListReq.class);
        if (req == null)
        {
            resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
            resp.setReturnMsg("请求为空错误");
            return resp;
        }
        if(req.getTimestamp() == null||req.getCompanyId()==null){
            resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
            resp.setReturnMsg("参数错误");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (req.getSecretKey() == null)
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("密钥为空");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (!validateSecretKey(req.getSecretKey(), req.getTimestamp()))
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("密钥错误");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (req.getUserId() == null)
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("用户ID为空");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (StringUtils.isBlank(req.getToken()) || !validateToken(req.getToken(), req.getUserId()))
        {
            resp.setReturnCode(ReturnCode.TOKEN_INVALID);
            resp.setReturnMsg("票据无效");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        list = (PaginationList<Zydlzd>) service.list(req.getDeviceName(), req.getCompanyId(), PwtzConstants.PORTAL_TYPE_APP,req.getMedLineCodes(),null, req.getBeginTime(), null, req.getPageSize(),req.getPage());
        if (list != null)
        {
            resp.setReturnCode(ReturnCode.SUCCESS);
            resp.setReturnMsg("查询低压电缆列表信息成功");
            resp.setTimestamp(req.getTimestamp());
            resp.setObjs(list);
        }else{
            resp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
            resp.setReturnMsg("查询低压电缆列表信息调用接口返回失败");
            resp.setTimestamp(req.getTimestamp());
        }
    }
    catch (PwtzDatabaseException e)
    {
        resp.setReturnCode(ReturnCode.DB_EXCEPTION);
        resp.setReturnMsg("数据库操作异常");
        resp.setTimestamp(req.getTimestamp());
    }
    catch (Exception e)
    {
        resp.setReturnCode(ReturnCode.UNKOWN_ERROR);
        resp.setReturnMsg("系统错误");
        resp.setTimestamp(req.getTimestamp());
    }
    return resp;
}

/**
 * 新增低压电缆信息
 * 一句话描述 <br/>
 * <p>Description: TODO(详细描述) 
 * <br/>
 * <p>Author: Administrator<br/>
 * <p>Date: 2014年9月25日-上午10:22:27<br/>
 * <p>
 * @param request
 * @return   
 *
 */
@RequestMapping(value = "/add", method = RequestMethod.POST)
@ResponseBody
public ObjResponse<Zydlzd> add(HttpServletRequest request)
{
    ObjResponse<Zydlzd> resp = new ObjResponse<Zydlzd>();
    ZydlzdObjReq req = null;
    try
    {
        req = (ZydlzdObjReq) getJsonToObj(request, ZydlzdObjReq.class);
        if (req == null)
        {
            resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
            resp.setReturnMsg("请求为空错误");
            return resp;
        }
        if(req.getTimestamp() == null||req.getObj()==null){
            resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
            resp.setReturnMsg("参数错误");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (req.getSecretKey() == null)
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("密钥为空");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (!validateSecretKey(req.getSecretKey(), req.getTimestamp()))
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("密钥错误");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (req.getUserId() == null)
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("用户ID为空");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (StringUtils.isBlank(req.getToken()) || !validateToken(req.getToken(), req.getUserId()))
        {
            resp.setReturnCode(ReturnCode.TOKEN_INVALID);
            resp.setReturnMsg("票据无效");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        int result=0;
        Zydlzd rtObj = null;
        if(service.load(((Zydlzd)req.getObj()).getDeviceid())!=null){
            result=service.update((Zydlzd)req.getObj());
        }else{
            rtObj=service.add((Zydlzd)req.getObj());
        }
        if (rtObj != null||result>0)
        {
            rtObj=service.load(((Zydlzd)req.getObj()).getDeviceid());
            daservice.add(DeviceDictConstants.ZYDLZD_MANUFACTURE_FACTORY_DICT_ID, ((Zydlzd)req.getObj()).getProductionfactory());
            daservice.add(DeviceDictConstants.ZYDLZD_MODEL_DICT_ID, ((Zydlzd)req.getObj()).getModel());
            resp.setReturnCode(ReturnCode.SUCCESS);
            resp.setReturnMsg("新增中压电缆终端成功");
            rtObj.setLastoperatorid(req.getUserId());
            service.update(rtObj);
            resp.setTimestamp(req.getTimestamp());
            setLog(rtObj.getDeviceid(), rtObj.getCompanyid(), AbstractController.中压电缆终端, AbstractController.add, "新增中压电缆终端["+rtObj.getDevicename()+"]成功", req.getUserId());
            resp.setObj(rtObj);
        }else{
            resp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
            resp.setReturnMsg("新增中压电缆终端调用接口返回失败");
            resp.setTimestamp(req.getTimestamp());
        }
    }
    catch (PwtzDatabaseException e)
    {
        resp.setReturnCode(ReturnCode.DB_EXCEPTION);
        resp.setReturnMsg("数据库操作异常");
        resp.setTimestamp(req.getTimestamp());
    }
    catch (Exception e)
    {
        resp.setReturnCode(ReturnCode.UNKOWN_ERROR);
        resp.setReturnMsg("系统错误");
        resp.setTimestamp(req.getTimestamp());
    }
    return resp;
}

/**
* 修改中压电缆分支箱信息
* 一句话描述 <br/>
* <p>Description: TODO(详细描述) 
* <br/>
* <p>Author: Administrator<br/>
* <p>Date: 2014年9月25日-上午10:22:47<br/>
* <p>
* @param request
* @return   
*
*/
@RequestMapping(value = "/update", method = RequestMethod.POST)
@ResponseBody
public ObjResponse<Zydlzd> update(HttpServletRequest request)
{
    ObjResponse<Zydlzd> resp = new ObjResponse<Zydlzd>();
    ZydlzdObjReq req = null;
    int result = 0;
    try
    {
        req = (ZydlzdObjReq) getJsonToObj(request, ZydlzdObjReq.class);
        if (req == null)
        {
            resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
            resp.setReturnMsg("请求为空错误");
            return resp;
        }
        if(req.getTimestamp() == null||req.getObj()==null){
            resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
            resp.setReturnMsg("参数错误");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (req.getSecretKey() == null)
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("密钥为空");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (!validateSecretKey(req.getSecretKey(), req.getTimestamp()))
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("密钥错误");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (req.getUserId() == null)
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("用户ID为空");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (StringUtils.isBlank(req.getToken()) || !validateToken(req.getToken(), req.getUserId()))
        {
            resp.setReturnCode(ReturnCode.TOKEN_INVALID);
            resp.setReturnMsg("票据无效");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        result = service.update((Zydlzd) req.getObj());
        if (result > 0)
        {
            Zydlzd rtObj=service.load(((Zydlzd) req.getObj()).getDeviceid());
            daservice.add(DeviceDictConstants.ZYDLZD_MANUFACTURE_FACTORY_DICT_ID, ((Zydlzd) req.getObj()).getProductionfactory());
            daservice.add(DeviceDictConstants.ZYDLZD_MODEL_DICT_ID, ((Zydlzd) req.getObj()).getModel());
            resp.setReturnCode(ReturnCode.SUCCESS);
            resp.setReturnMsg("修改中压电缆终端成功");
            rtObj.setLastoperatorid(req.getUserId());
            service.update(rtObj);
            resp.setTimestamp(req.getTimestamp());
            resp.setObj(rtObj);
            setLog(((Zydlzd) req.getObj()).getDeviceid(), ((Zydlzd) req.getObj()).getCompanyid(), AbstractController.中压电缆终端, AbstractController.update, "修改中压电缆终端["+((Zydlzd) req.getObj()).getDevicename()+"]成功", req.getUserId());
        }else{
            resp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
            resp.setReturnMsg("修改中压电缆终端调用接口返回失败");
            resp.setTimestamp(req.getTimestamp());
        }
    }
    catch (PwtzDatabaseException e)
    {
        resp.setReturnCode(ReturnCode.DB_EXCEPTION);
        resp.setReturnMsg("数据库操作异常");
        resp.setTimestamp(req.getTimestamp());
    }
    catch (Exception e)
    {
        resp.setReturnCode(ReturnCode.UNKOWN_ERROR);
        resp.setReturnMsg("系统错误");
        resp.setTimestamp(req.getTimestamp());
    }
    return resp;
}

/**
 * 删除低压电缆信息
 * 一句话描述 <br/>
 * <p>Description: TODO(详细描述) 
 * <br/>
 * <p>Author: Administrator<br/>
 * <p>Date: 2014年9月25日-上午10:23:18<br/>
 * <p>
 * @param request
 * @return   
 *
 */
 
@RequestMapping(value = "/delete", method = RequestMethod.POST)
@ResponseBody
public ObjResponse<Zydlzd> delete(HttpServletRequest request)
{
    ObjResponse<Zydlzd> resp = new ObjResponse<Zydlzd>();
    BaseIdReq req = null;
    int result = 0;
    try
    {
        req = (BaseIdReq) getJsonToObj(request, BaseIdReq.class);
        if (req == null)
        {
            resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
            resp.setReturnMsg("请求为空错误");
            return resp;
        }
        if(req.getTimestamp() == null||req.getDeviceId()==null){
            resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
            resp.setReturnMsg("参数错误");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (req.getSecretKey() == null)
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("密钥为空");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (!validateSecretKey(req.getSecretKey(), req.getTimestamp()))
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("密钥错误");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (req.getUserId() == null)
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("用户ID为空");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (StringUtils.isBlank(req.getToken()) || !validateToken(req.getToken(), req.getUserId()))
        {
            resp.setReturnCode(ReturnCode.TOKEN_INVALID);
            resp.setReturnMsg("票据无效");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        result = service.delete(req.getDeviceId());
        if (result > 0)
        {
            Zydlzd rtObj=service.load(req.getDeviceId());
            resp.setReturnCode(ReturnCode.SUCCESS);
            resp.setReturnMsg("删除中压电缆终端成功");
            rtObj.setLastoperatorid(req.getUserId());
            service.update(rtObj);
            resp.setObj(rtObj);
            resp.setTimestamp(req.getTimestamp());
            setLog(req.getDeviceId(), rtObj.getCompanyid(), AbstractController.中压电缆终端, AbstractController.delete, "删除中压电缆终端["+rtObj.getDevicename()+"]成功", req.getUserId());
        }else{
            resp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
            resp.setReturnMsg("删除中压电缆终端调用接口返回失败");
            resp.setTimestamp(req.getTimestamp()); 
        }
    }
    catch (CanNotDeleteException e)
    {
        resp.setReturnCode(ReturnCode.DB_EXCEPTION);
        resp.setReturnMsg("存在依耐关系，不能删除");
        resp.setTimestamp(req.getTimestamp());
    }
    catch (PwtzDatabaseException e)
    {
        resp.setReturnCode(ReturnCode.DB_EXCEPTION);
        resp.setReturnMsg("数据库操作异常");
        resp.setTimestamp(req.getTimestamp());
    }
    catch (Exception e)
    {
        resp.setReturnCode(ReturnCode.UNKOWN_ERROR);
        resp.setReturnMsg("系统错误");
        resp.setTimestamp(req.getTimestamp());
    }
    return resp;
}
}
